package com.nantian.common.file;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * <p>ClassName: FileUtils</p >
 * <p>Description: 文件工具类</p >
 * <p>Copyright: Copyright©2014</p >
 * <p>Company: 广州南天电脑系统有限公司</p >
 * <p>Date: 2020-12-25</p >
 *
 * @author 刘晓辉
 * @version 1.0
 */
public class FileUtils {
    private static Logger log = LoggerFactory.getLogger(FileUtils.class);

    /**
     * 文件分割符
     */
    public final static String SEPARATOR = File.separatorChar == '\\' ? "\\\\" : File.separator;
    /**
     * 文件后缀名分割符
     */
    public final static String FILE_NAME_SUFFIX = ".";

    /**
     * linux 文件路径分割符
     */
    public final static String LINUX_SEPARATOR = "/";
    /**
     * windows 文件路径分割符
     */
    public final static String WINDOWS_SEPARATOR = "\\";
    
    /**
     * 获取文件的后缀
     * @param fileFullPath 文件完整路径
     * @return 文件后缀
     */
    public static String getFileType(String fileFullPath) {
        int index = fileFullPath.indexOf(FILE_NAME_SUFFIX);
        if (index == -1) {
            return "";
        }
        String fileType = fileFullPath.substring(index + 1);
        return fileType;
    }

    /**
     * 获取文件目录
     * @param fileFullPath 完成的文件路径
     * @return 文件所在目录
     */
    public static String getFileDirectory(String fileFullPath) {
        String separator = null;
        if (fileFullPath.contains(WINDOWS_SEPARATOR)) {
            //win 路径写法
            separator = WINDOWS_SEPARATOR;
        } else if (fileFullPath.contains(LINUX_SEPARATOR)) {
            //linux/unix 路径写法
            separator = LINUX_SEPARATOR;
        } else {
            throw new IllegalArgumentException("非法文件路径【" + fileFullPath + "】");
        }
        int index = fileFullPath.lastIndexOf(separator) + 1;
        String dir = fileFullPath.substring(0, index);
        return dir;
    }
}
